<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" inline size="small">
            <el-form-item>
              <el-select v-model="queryData.surchargeId" filterable clearable placeholder="附加费方案名称">
                <el-option v-for="item in surchargeOptions" :key="item.value" :value="item.value" :label="item.name" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryData.status" filterable clearable placeholder="状态">
                <el-option v-for="item in surchargeStatusOptions" :key="item.code" :value="item.code" :label="item.name" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="list">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button type="primary" @click="add">新增</el-button>
        </div>
        <el-table :data="table_body" :border="true" :max-height="tableHeight">
          <el-table-column prop="title" label="附加费方案名称" min-width="100" align="center" />
          <el-table-column label="状态" min-width="100" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.status ==='未启用'" style="color: red">未启用</span>
              <span v-else style="color: #4298ef">已启用</span>
            </template>
          </el-table-column>
          <el-table-column prop="effectiveFrom" label="生效时间" min-width="100" align="center" />
          <el-table-column prop="effectiveTo" label="失效时间" min-width="100" align="center" />
          <el-table-column prop="createdBy" label="创建时间" min-width="100" align="center" />
          <el-table-column prop="createdAt" label="创建时间" min-width="100" align="center" />
          <el-table-column prop="createdBy" label="操作人" min-width="100" align="center" />
          <el-table-column prop="editedAt" label="最后更新时间" min-width="100" align="center" />
          <el-table-column label="操作" min-width="100" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="edit(scope.row.logId)">编辑</el-button>
              <el-button v-if="scope.row.status == '未启用'" type="text" @click="enable(scope.row)">启用</el-button>
              <el-button v-else type="text" style="color: red" @click="disabled(scope.row)">禁用</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <SurchargeDetails ref="surchargeDetails" @query="list" />
  </div>
</template>

<script>
import request from '@/utils/request'
import { mapState } from 'vuex'
import SurchargeDetails from './surcharge_details'

export default {
  components: { SurchargeDetails },
  data() {
    return {
      queryData: {
        pageSize: 50,
        currentPage: 1,
        zoneName: '',
        editor: '',
        region: '',
        status: '',
        surchargeId: ''
      },
      surchargeStatusOptions: [
        { code: '0', name: '未启用' },
        { code: '1', name: '已启用' }
      ],
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      table_body: [],
      table_header: [
        { key: 'title', title: '附加费方案名称', minWidth: 100, align: 'center' },
        { slot: 'status', title: '状态', minWidth: 100, align: 'center' },
        { key: 'effectiveFrom', title: '生效时间', minWidth: 100, align: 'center' },
        { key: 'effectiveTo', title: '失效时间', minWidth: 100, align: 'center' },
        { key: 'createdBy', title: '创建时间', minWidth: 100, align: 'center' },
        { key: 'createdAt', title: '创建时间', minWidth: 100, align: 'center' },
        { key: 'createdBy', title: '操作人', minWidth: 100, align: 'center' },
        { key: 'editedAt', title: '最后更新时间', minWidth: 100, align: 'center' },
        { slot: 'operation', title: '操作', minWidth: 100, align: 'center' }
      ],
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      visible: false,
      Lookover: true
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 300
    },
    pageSize() {
      return this.pageSizeOpts[0]
    },
    ...mapState(//
      {
        surchargeOptions: state => state.base.surchargeOptions
      }
    )
  },
  created() {
    this.list()
    if (this.surchargeOptions.length === 0) {
      this.$store.dispatch('getSurchargOptions')
    }
  },
  methods: {
    list() {
      request({
        url: `/channel/surcharge/query`,
        method: 'post',
        data: this.queryData
      }).then(res => {
        if (res.success) {
          this.pageTotal = res.data.total
          this.table_body = res.data.records
        }
      })
    },
    add() {
      this.$refs.surchargeDetails.clean()
      this.$refs.surchargeDetails.show()
    },
    enable(row) {
      const _this = this
      this.$confirm('确认启用吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        request({
          url: `/channel/surcharge/enable?logId=` + row.logId,
          method: 'get'
        }).then(res => {
          if (res.success) {
            _this.list()
            _this.$store.dispatch('getSurchargOptions')
          }
        })
      })
    },
    disabled(row) {
      const _this = this
      this.$confirm('确认停用吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        request({
          url: `/channel/surcharge/disabled?logId=` + row.logId,
          method: 'get'
        }).then(res => {
          if (res.success) {
            _this.list()
            _this.$store.dispatch('getSurchargOptions')
          }
        })
      })
    },
    edit(logId) {
      this.$refs.surchargeDetails.edit(logId)
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.queryData.resetCurrentPage = false
      this.list()
    }
  }
}
</script>
